import store from '@/store';
import Vue from 'vue';
import VueRouter from 'vue-router';
import Home from '../views/Home.vue';

// 把这段代码直接粘贴到router/index.js中的Vue.use(VueRouter)之前
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch((err) => {});
};
Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    component: Home,
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
  },
  {
    path: '/Goods',
    name: 'Goods',
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/Goods.vue'),
  },
  {
    path: '/User',
    name: 'User',
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/User.vue'),
  },
  {
    path: '/Order',
    name: 'Order',
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/Free.vue'),
  },
  {
    path: '/Free',
    name: 'Free',
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/Free.vue'),
  },
  {
    path: '/Home',
    name: 'Home',
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/Home.vue'),
  },
  {
    path: '/Details',
    name: 'Details',
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/Details.vue'),
  },
  {
    path: '/user',
    name: 'user',
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/User.vue'),
    redirect: '/user/cart',
    children: [
      //    /user/cart
      { path: 'cart', component: () => import('../components/user/Cart.vue') },
    ],
  },
  {
    path: '*',
    name: 'Error',
    component: () => import('../views/Error.vue'),
  },
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
});

// beforeEach((to, from, next) => {
//   console.log('to1:', yo, 'from1:', from);
//   let token = localStorage.getItem('x-auth-token');
//   if (token) {
//     next();
//   } else {
//     store.dispatch('showToast/asyncIsShowToast', {
//       msg: '你还没有登录',
//       type: 'warning',
//     });
//   }
// });

export default router;
